#include "comm.h"

bool MakeFifo()
{
    int n = mkfifo(FILENAME, 0666);
    if(n < 0)
    {
        perror("mkfifo");
        return false;
    }
    std::cout << "creat fifo success" << std::endl;
    return true;
}
int main()
{
Start:
    int rfd = open(FILENAME, O_RDONLY);
    if(rfd < 0)
    {
        perror("open");
        if(MakeFifo()) goto Start;
        return -1;
    } 
    std::cout << "open fifo success" << std::endl;

    char buffer[1024];
    while(true)
    {
        ssize_t n = read(rfd, buffer, sizeof(buffer) - 1);
        if(n > 0)
        {
            buffer[n] = 0;
            std::cout << buffer << std::endl;
        }
        else
        {
            break;
        }
    }
}